package com.kehutong.work.order.manage.service;

import com.kehutong.work.order.manage.entity.WorkOrderManageClassify;
import com.kehutong.work.order.manage.entity.WorkOrderManageType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindArray;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;

@WebService("/work/order/manage/type/service/api")
public class WorkOrderClassifyServiceAPI {

    @Inject
    private JdbcSession jdbcSession;
    @ReqMapping("/page")
    public Object listPage(JSONObject jsonObject) {
        FindPage<WorkOrderManageClassify> findPage = jdbcSession.findPage(WorkOrderManageClassify.class);

        String parentId = jsonObject.getString("parentId");
        if ("0".equals(parentId)) {// 0表示拉取一级+二级全部数据
            // continue
        } else if (Objects.nonEmpty(parentId)) {
            findPage.eq(jsonObject, "parentId");
        } else {
            findPage.markBegin();
            findPage.isNull("parentId").or().isEmpty("parentId");
            findPage.markEnd();
        }

        return Result.success(findPage
                                 .like(jsonObject, "name")
                                 .eq("deleted", false)
                                 .order("enable", "desc")
                                 .order("sort", "asc")
                                 .page(jsonObject)
                                 .exe());
}

    public Object list(JSONObject jsonObject) {
        FindArray<WorkOrderManageClassify> findArray = jdbcSession.findArray(WorkOrderManageClassify.class)
                                                                  .eq(jsonObject, "name", "typeId");

        String parentId = jsonObject.getString("parentId");
        if ("0".equals(parentId)) {// 0表示拉取一级+二级全部数据
            // continue
        } else if (Objects.nonEmpty(parentId)) {
            findArray.eq(jsonObject, "parentId");
        } else {
            findArray.markBegin();
            findArray.isNull("parentId").or().isEmpty("parentId");
            findArray.markEnd();
        }

        findArray.eq("deleted", false)
                 .eq("enable", true);

        if ("0".equals(parentId)){
            findArray.order("parentId", "asc");
        }
        findArray.order("sort", "asc");

        return Result.success(findArray.exe());
    }

    public Object listEnable(JSONObject jsonObject) {
        FindArray<WorkOrderManageClassify> findArray = jdbcSession.findArray(WorkOrderManageClassify.class)
                                                                  .eq(jsonObject, "name", "typeId");

        String parentId = jsonObject.getString("parentId");
        if ("0".equals(parentId)) {// 0表示拉取一级+二级全部数据
            // continue
        } else if (Objects.nonEmpty(parentId)) {
            findArray.eq(jsonObject, "parentId");
        } else {
            findArray.markBegin();
            findArray.isNull("parentId").or().isEmpty("parentId");
            findArray.markEnd();
        }

        findArray.eq("deleted", false);

        if ("0".equals(parentId)){
            findArray.order("parentId", "asc");
        }
        findArray.order("sort", "asc");

        return Result.success(findArray.exe());
    }

    public Object save(WorkOrderManageClassify classify) {
        if (Objects.isEmpty(classify.getTypeId())) {
            return Result.error(0, "请选择所属类型");
        }

        if (checkName(classify)) {
            return Result.error(0, "同一所属类型下分类名称不能重复，请检查参数后重试");
        }
        setTypeName(classify);
        setParentName(classify);
        jdbcSession.insert(classify);

        return Result.success();
    }


    public Object update(WorkOrderManageClassify classify) {
        if (checkName(classify)) {
            return Result.error(0, "同一所属类型下分类名称不能重复，请检查参数后重试");
        }
        setTypeName(classify);
        setParentName(classify);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    private boolean checkName(WorkOrderManageClassify classify) {
        WorkOrderManageClassify temp = jdbcSession.findOne(WorkOrderManageClassify.class)
                                                  .eq("name", classify.getName())
                                                  .eq("typeId", classify.getTypeId())
                                                  .eq("deleted", false)
                                                  .exe();

        if (Objects.nonNull(temp)) {
            if (classify.getId().equals(temp.getId())) {
                return false;
            }

            return true;
        }

        return false;
    }

    private void setTypeName(WorkOrderManageClassify classify) {
        WorkOrderManageType type = Pool.get(WorkOrderManageType.class, classify.getTypeId());
        if (Objects.nonNull(type)) {
            classify.setTypeName(type.getName());
        }
    }

    private void setParentName(WorkOrderManageClassify classify) {
        if (Objects.isEmpty(classify.getParentId())) {
            List<WorkOrderManageClassify> childList = jdbcSession.findArray(WorkOrderManageClassify.class)
                                                                 .eq("parentId", classify.getId())
                                                                 .exe();
            childList.forEach(e -> {
                e.setParentName(classify.getName());
                jdbcSession.updateById(e);
            });
        } else {
            WorkOrderManageClassify parent = Pool.get(WorkOrderManageClassify.class, classify.getParentId());
            if (Objects.nonNull(parent)) {
                classify.setParentName(parent.getName());
            }
        }


    }

    public Object delete(WorkOrderManageClassify classify) {
        if (Objects.isEmpty(classify.getParentId())) {
            List<WorkOrderManageClassify> childList = jdbcSession.findArray(WorkOrderManageClassify.class)
                                                                 .eq("parentId", classify.getId())
                                                                 .eq("deleted", false)
                                                                 .exe();
            if (childList.size() > 0) {
                return Result.error(0, "请先删除子分类");
            }
        }

        classify.setDeleted(true);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object updateSort(WorkOrderManageClassify classify, Integer sort) {
        classify.setSort(sort);
        jdbcSession.updateById(classify);

        return Result.success();
    }

}
